// TianguoguoXiaopu-master/pages/sys/wode/notice/addnotice.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    noticeData:{
      title:"",
      contentL:"",
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    that.setData({
      wxaddressstatus:false
    })
    
    var id1 = e.id;
    if (id1) {
      // 初始化原数据
      wx.showLoading();
      const db = wx.cloud.database()
      db.collection('noticeList').where({
        _id: id1
      }).get({
        success: function (res) {
        
          wx.hideLoading();
          if (res.data.length > 0) {
            
            that.setData({
              updateid: res.data[0]._id,
              noticeData: res.data[0],
              //state: { province: res.data[0].provinceStr, city: res.data[0].cityStr,
              //  area:res.data[0].districStr},
              
             
            });
            
            return;
          } else {
            wx.showModal({
              title: '提示',
              content: '无法获取数据',
              showCancel: false
            })
          }
        }
      })
      
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  contentInput(e){
    this.data.noticeData.content = e.detail.value
   
  },

  titleInput(e){
    this.data.noticeData.title = e.detail.value
   
  },
   //保存
   save: function (e) {
      
    console.log(e)
    var that = this;
   
    

    if (!that.data.noticeData.title) {
      wx.showModal({
        title: '提示',
        content: '请填写标题',
        showCancel: false
      })
      return
    }
    if (!that.data.noticeData.content ) {
      wx.showModal({
        title: '提示',
        content: '请填写内容',
        showCancel: false
      })
      return
    }
   
   
  
   
    const db = wx.cloud.database()
    if(!that.data.updateid){
        db.collection('noticeList').add({
          data: {
            title:that.data.noticeData.title,
            content:that.data.noticeData.content,
            dateAdd:new Date(),
          },
          success: function(res) {
           
            wx.navigateBack({})
          }
        })
   }else{
    
    
    db.collection('noticeList').doc(that.data.updateid).update({
      data: {
        title:that.data.noticeData.title,
            content:that.data.noticeData.content,
      },
      success: function(res) {
       
        wx.navigateBack()
      }
    })
   }
  
  },
  
  delete: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除该公告吗？',
      success: function (res) {
        if (res.confirm) {
          const db = wx.cloud.database()
        
          db.collection('noticeList').doc(id).remove({
            
            success: function(res) {
              wx.navigateBack({
                delta: 1,
              })
            }
          })
          
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})